<template>
	<view class="index-page">
		<u-swiper :list="swiperList" height="300rpx" radius="0" indicatorStyle="right: 20px" circular :autoplay="swiperAutoplay"
			@change="swiperChange">
			<view slot="indicator" class="indicator-num">
				<view class="left">
					<text>{{ currentNum + 1 }}</text>
				</view>
				<view class="right">
					<text>{{ swiperList.length }}</text>
				</view>
			</view>
		</u-swiper>
		

		<view class="phone-box mt-20 flex items-center justify-between">
			<view class="text flex items-center">
				<view class="label">
					<view>举报热线</view>
					<view class="subtitle">ཐེར་འདོན་ཁ་པར།</view>
				</view>
				<view class="number">{{this_app_config.tcp_phone}}</view>
			</view>
			<button size="mini" type="default" class="btn" @click="makePhoneCall">
				<text>拨打电话</text>
				<text class="right">ཁ་པར་རྒྱག་པ་</text>
			</button>
		</view>

		<view class="form-box mt-20">
			<!-- <view class="form-item">
				<view class="label-box">
					<text class="required">*</text>
					<text class="label">举报类目</text>
					<text class="tibetan">（ཐེར་འདོན་རིགས་ཀྱི་དཀར་ཆག）</text>
				</view>
				<view class="input-box flex items-center" @click="categoryShow = true">
					<input class="flex-1" type="text" v-model="form.category+form.category_en" disabled
						placeholder="请选择您举报的类目 ཁྱེད་རང་གིས་ཐེར་འདོན་རིགས་ཀྱི་དཀར་ཆག་བདམས་རོགས།">
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="form-item">
				<view class="label-box">
					<text class="required">*</text>
					<text class="label">举报来源</text>
					<text class="tibetan">（ཐེར་འདོན་ཡོང་ཁུངས།）</text>
				</view>
				<view class="input-box flex items-center" @click="sourceShow = true">
					<input class="flex-1" type="text" v-model="form.source+form.source_en" disabled
						placeholder="请选择您举报的来源 ཁྱེད་རང་གི་ཐེར་འདོན་ཡོང་ཁུངས་བདམས་རོགས།">
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view> -->
			<view class="form-item">
				<view class="label-box">
					<text class="required">*</text>
					<text class="label">举报链接</text>
					<text class="tibetan">（ཐེར་འདོན་འབྲེལ་མཐུད།）</text>
				</view>
				<view class="input-box flex items-center">
					<input class="flex-1" type="text" v-model="form.link"
						placeholder="请填写您举报的链接 ཁྱེད་རང་གི་ཐེར་འདོན་འབྲེལ་མཐུད་འབྲི་རོགས།">
				</view>
			</view>
			<view class="form-item">
				<view class="label-box">
					<text class="required">*</text>
					<text class="label">举报证据</text>
					<text class="tibetan">（ཐེར་འདོན་དཔང་རྟགས།）</text>
				</view>
				<view class="upload-box mt-20">
					<u-upload accept="media" :fileList="form.images" multiple :maxCount="10" width="296rpx"
						height="216rpx" @afterRead="afterRead" @delete="deletePic" @onPreviewVideo="onPreviewVideo">
						<view class="upload-default flex flex-col items-center justify-center">
							<u-icon name="plus" color="#c0c0c0" size="60rpx"></u-icon>
							<view class="text">
								<view>请上传图片或视频</view>
								<view class="tibetan">པར་རིས་སམ་བརྙན་ལམ་བརྒྱུད་སྐུར་གནང་རོགས།</view>
							</view>
						</view>
					</u-upload>
				</view>
			</view>
			<view class="form-item">
				<view class="label-box">
					<text class="label">其他</text>
					<text class="tibetan">（གཞན་དག）</text>
				</view>
				<view class="input-box flex items-center">
					<input class="flex-1" type="text" v-model="form.other"
						placeholder="请填写建议或其他证据 བསམ་འཆར་རམ་ཡང་ན་དཔང་རྟགས་གཞན་དག་འབྲི་རོགས།">
				</view>
			</view>
		</view>

		<view class="button-box">
			<button class="btn" @click="completedForm">提交(འབུལ་བ།)</button>
		</view>
		<u-picker :show="categoryShow" :columns="categoryColumns" itemHeight="56" @cancel="categoryShow = false"
			@confirm="categoryConfirm"></u-picker>
		<u-picker :show="sourceShow" :columns="sourceColumns" @cancel="sourceShow = false" @confirm="sourceConfirm">
		</u-picker>
		
		<view class="tabbar-placeholder"></view>
	</view>
</template>

<script>
	const util = require('../../common/util.js');
	const data = require('../../common/data.js');
	export default {
		data() {
			return {
				this_app_config: {},
				currentNum: 0,
				swiperList: [],
				swiperAutoplay: true,
				form: {
					category: '',
					category_en: '',
					source: '',
					source_en: '',
					link: '',
					images: [],
					other: ''
				},
				// 黄、赌、毒、暴力
				categoryShow: false,
				categoryColumns: [
					[{
						id: 0,
						text: '政治类有害信息',
						tibetan: 'ཆབ་སྲིད་རིགས་ཀྱི་གནོད་ལྡན་ཆ་འཕྲིན།'
					}, {
						id: 1,
						text: '民族类有害信息',
						tibetan: 'མི་རིགས་རིགས་ཀྱི་གནོད་ལྡན་ཆ་འཕྲིན།'
					}, {
						id: 2,
						text: '暴恐类',
						tibetan: 'འཇིག་གཏུམ་རིགས།'
					}, {
						id: 3,
						text: '公序良俗类不良信息',
						tibetan: 'སྤྱི་སྒྲིག་སྲོལ་བཟང་རིགས་ཀྱི་ཆ་འཕྲིན་མི་ལེགས་པ།'
					}, {
						id: 4,
						text: '淫秽色情类',
						tibetan: 'འདོད་ལོག་ཆགས་སྲིད་ཀྱི་རིགས།'
					}, {
						id: 5,
						text: '宗教类有害信息',
						tibetan: 'ཆོས་ལུགས་རིགས་ཀྱི་གནོད་ལྡན་ཆ་འཕྲིན།'
					}, {
						id: 6,
						text: '侵权类信息',
						tibetan: 'དབང་གནོད་རིགས་ཀྱི་ཆ་འཕྲིན།'
					}, {
						id: 7,
						text: '涉达赖分裂集团类',
						tibetan: 'ཏཱ་ལའི་ཁ་ཕྲལ་རུ་ཚོགས་དང་འབྲེལ་བ་ཡོད་པའི་རིགས།'
					}, {
						id: 8,
						text: '其他类',
						tibetan: 'རིགས་གཞན་དག'
					}, {
						id: 9,
						text: '互联网专项整治的违法和不良信息',
						tibetan: 'དྲ་སྦྲེལ་ཆེད་དོན་བཅོས་སྒྲིག་གི་ཁྲིམས་འགལ་དང་ཆ་འཕྲིན་མི་ལེགས་པ།'
					}]
				],
				sourceShow: false,
				// 网站、微信、抖音、微博、其他平台
				sourceColumns: [
					[{
						id: 0,
						text: '网站',
						tibetan: 'དྲ་ཚིགས་།'
					}, {
						id: 1,
						text: '微信',
						tibetan: 'འཕྲིན་ཕྲན།',

					}, {
						id: 2,
						text: '抖音',
						tibetan: 'ཁྱུག་དབྱེངས།',
					}, {
						id: 3,
						text: '微博',
						tibetan: 'པོད་འཕྲིན།',
					}, {
						id: 4,
						text: '其他平台',
						tibetan: 'སྟེགས་བུ་གཞན་དག',
					}]
				],
			}
		},
		onShow() {
			// #ifdef MP-WEIXIN
			if (typeof this.$mp.page.getTabBar === 'function' && this.$mp.page.getTabBar()) {
				this.$mp.page.getTabBar().setData({
					selected: 0
				})
			}
			// #endif
		},
		onLoad() {
			this.loadData();
		},
		methods: {
			completedForm() {
				let that = this;
				var requsetData = {};
				requsetData.form = JSON.stringify(that.form);
				util.requestPost(
					'/Index/applyAction',
					requsetData,
					function(res) {
						uni.showModal({
							title: '提示',
							content: res.msg,
							showCancel: false,
							success: function(res) {
								that.form = {
									category: '',
									category_en: '',
									source: '',
									source_en: '',
									link: '',
									images: [],
									other: ''
								}
								uni.navigateTo({
									url: '/pages/my/examine?a_status=0'
								})
							}
						});
					},
					'', {
						isShowLoading: false
					}
				);
			},
			loadData: function() {
				let that = this;
				var requsetData = {};
				util.requestPost(
					'/Index/index',
					requsetData,
					function(res) {
						that.this_app_config = res.data.this_app_config;
						that.swiperList = res.data.this_ad_list;
						that.this_user_info = res.data.this_user_info;
					},
					'', {
						isShowLoading: false
					}
				);
			},
			swiperChange(e) {
				this.currentNum = e.current
				
				if ((e.current + 1) === this.swiperList.length) {
					this.swiperAutoplay = false
				}
			},
			makePhoneCall() {
				var phone = this.this_app_config.tcp_phone
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			categoryConfirm(e) {
				this.form.category = e.value[0].text;
				this.form.category_en = e.value[0].tibetan;
				this.categoryShow = false
			},
			sourceConfirm(e) {
				this.form.source = e.value[0].text
				this.form.source_en = e.value[0].tibetan
				this.sourceShow = false
			},
			// 删除文件
			deletePic(event) {
				this.form.images.splice(event.index, 1)
			},
			// 新增文件
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.form.images.length
				lists.map((item) => {
					this.form.images.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.form.images[fileListLen]
					this.form.images.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				var that = this;
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: data.jubao_url_prefix + '/upload/uploadFile', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							const data = JSON.parse(res.data)
							if (data.code == 1) {
								setTimeout(() => {
									resolve(data.data.url)
								}, 1000)
							} else {
								uni.showToast({
									icon: 'none',
									title: data.msg,
									position: "bottom"
								});
								this.form.images.splice(this.form.images.length - 1, 1)
								return;
							}

						},
						fail(e) {
							uni.showToast({
								icon: 'none',
								title: e,
								position: "bottom"
							});
						}
					});
				})
			},
			onPreviewVideo(e) {
				const url = e.url
				uni.navigateTo({
					url: '/pages/my/video?url=' + url
				})
			}
		}
	}
</script>

<style lang="scss">
	.index-page {
		min-height: 100%;
		background-color: $bg-color;

		.phone-box {
			padding: 20rpx 36rpx;
			background-color: #fff;

			.text {
				font-size: 30rpx;

				.label {
					color: $primary-color;
					text-align: center;
				}

				.subtitle {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					margin-left: 20rpx;
				}
			}

			.btn {
				margin: 0;
				color: #fff;
				background-color: $primary-color;

				.right {
					margin-left: 4rpx;
					font-size: 24rpx;
				}
			}
		}

		.form-box {
			padding: 30rpx 36rpx;
			background-color: #fff;

			.form-item {
				&:not(:last-of-type) {
					margin-bottom: 32rpx;
				}

				.label-box {
					.required {
						color: $red-color;
					}

					.label {
						font-size: 30rpx;
						font-weight: bold;
					}

					.tibetan {
						font-size: 24rpx;
					}
				}

				.input-box {
					margin-top: 20rpx;
					padding: 24rpx 14rpx;
					border-radius: 8rpx;
					background-color: #f8f8f8;

					.input-placeholder {
						font-size: 22rpx;
					}
				}

				.upload-default {
					width: 470rpx;
					height: 216rpx;
					border-radius: 8rpx;
					background-color: #f8f8f8;

					.text {
						margin-top: 16rpx;
						color: #888;
						font-size: 30rpx;
						text-align: center;

						.tibetan {
							font-size: 24rpx;
							margin-top: 4rpx;
						}
					}
				}
			}
		}

		.button-box {
			padding: 40rpx 70rpx;

			.btn {
				color: #fff;
				font-size: 32rpx;
				height: 86rpx;
				line-height: 86rpx;
				border-radius: 86rpx;
				background-color: $primary-color;
			}
		}
	}
</style>
